package com.zhangtai.modules.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
//import com.zhangtai.modules.dao.ssqlDao.JobDaoMssql;
import com.zhangtai.modules.dto.AttendDto;
import com.zhangtai.modules.entity.attend.AttendSupplementEntity;
import com.zhangtai.modules.service.attend.AttendSupplementService;
import com.zhangtai.modules.vo.AttendVo;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("attend")
@Slf4j
@Api(tags="员工自助考勤")
@RestController()
public class AttendMemberController {
//    @Autowired
//    private JobDaoMssql jobDaoMssql;
    @Autowired
    private MemberLoginController memberLoginController;
    @Autowired
    private AttendSupplementService attendSupplementService;

    @PostMapping("getPersonAttendList")
    @ApiOperation("个人考勤列表")
    public R<List<AttendDto>> getPersonAttendList(@RequestBody @Valid AttendVo vo, HttpServletRequest request)throws Exception{
       JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
       String name = userInfo.getString("name");
       String code = userInfo.getString("usercode");
       Long orgId = userInfo.getLong("orgId");
       Date date = new Date();
       String dateStr = DateUtils.format(date,"yyyy-MM")+"-01";
       String endDate =DateUtils.format(DateUtils.addDateMonths(date,1),"yyyy-MM")+"-01" ;
       if(!ObjectUtils.isEmpty(vo.getStartDate())){
           dateStr = DateUtils.format(vo.getStartDate(),"yyyy-MM-dd");

       }
       if(!ObjectUtils.isEmpty(vo.getEndDate())){
           endDate =DateUtils.format(vo.getEndDate(),"yyyy-MM-dd");
       }
        List<AttendDto> attendDtoList = new ArrayList<>();
//        jobDaoMssql.getAttendDtoList(dateStr+" 00:00:00",endDate+" 04:00:00",orgId,code);

        return R.ok(attendDtoList);
    }


    @PostMapping("attendSupplement")
    @ApiOperation("补卡")
    public R attendSupplement(@RequestBody @Valid List<AttendSupplementEntity> voList){
        for(AttendSupplementEntity vo:voList){
            attendSupplementService.saveE(vo);
        }
        // TODO: 2020/12/3  将调用东软接口进行补卡
        return R.ok();
    }
}
